package cn.com.yto56.coresystem.business.msg.web.controller;

import cn.com.yto56.coresystem.business.msg.web.biz.IYtoMsgBiz;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

/**
 * Created by wangrui on 2017/6/5.
 */
@Component
@EnableWebSocket
public class WebSocketConfig extends WebMvcConfigurerAdapter implements WebSocketConfigurer {
    @Autowired
    private IYtoMsgBiz ytoMsgBizImpl;

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        //允许连接的域,只能以http或https开头
        String[] allowsOrigins = {"*"};
        registry.addHandler(ytoMsgWebSocketHandler(), "/websocket").setAllowedOrigins(allowsOrigins).addInterceptors(myInterceptor());
        registry.addHandler(ytoMsgWebSocketHandler(), "/sockjs/websocket").setAllowedOrigins(allowsOrigins).addInterceptors(myInterceptor()).withSockJS();
    }

    @Bean
    public YtoMsgWebSocketHandler ytoMsgWebSocketHandler() {
        return new YtoMsgWebSocketHandler(ytoMsgBizImpl);
    }

    @Bean
    public WebSocketHandshakeInterceptor myInterceptor() {
        return new WebSocketHandshakeInterceptor();
    }

}
